package com.wy.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wy.entity.GoodsDeposit;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface GoodsDepositMapper extends BaseMapper<GoodsDeposit> {

    /**
     * 72小时未支付 扣取保证金
     * @return
     */
    @Update("update goods_deposit set goods_deposit.`status` = 3 WHERE \n" +
            "user_id  in " +
            "(SELECT buyer_user_id " +
            "FROM goods_order WHERE `status` = 1 and timestampdiff(hour,goods_order.create_time,sysdate()) > 72)\n" +
            "and goods_id in " +
            "(SELECT goods_id " +
            "FROM goods_order WHERE `status` = 1 and timestampdiff(hour,goods_order.create_time,sysdate()) > 72)")
    int updateDeductDeposit();

    /**
     * 添加保证金
     * @param goodsDeposit 保证金信息
     * @return
     */
    @Insert("insert into goods_deposit" +
            "(deposit_id,user_id,goods_id,create_time.update_time,amount)" +
            "values (#{goodsDeposit.depositId},#{goodsDeposit.userId}," +
            "#{goodsDeposit.goodsId},#{goodsDeposit.createTime},#{goodsDeposit.updateTime},#{goodsDeposit.amount})")
    int addDeposit(GoodsDeposit goodsDeposit);

    /**
     * 退还保证金
     * @param userId 用户id
     * @param goodsId 商品id
     * @return
     */
    @Update(" update user_info,goods_deposit set balance = balance + goods_deposit.amount,goods_deposit.`status` = \"2\"\n" +
            " WHERE user_info.user_id = goods_deposit.user_id\n" +
            " AND goods_deposit.user_id = #{userId}\n" +
            " AND goods_deposit.goods_id = #{goodsId}\n" +
            " AND goods_deposit.`status` = \"1\"")
    int RefundDeposit(Long userId,Long goodsId);
}